package com.douwong.student.activity;


import android.content.Context;
import android.content.Intent;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.view.View.OnFocusChangeListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.douwong.student.R;
import com.douwong.student.base.BaseActivity;
import com.douwong.student.contant.CommonConfig;
import com.douwong.student.fragment.ClassNoticeRecordFragment;
import com.douwong.student.model.SubjectObject;
import com.douwong.student.view.CommSearch;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 课堂文件
 * 
 * @author leihuajie
 *
 */
public class ClassFileActivity extends BaseActivity {
	private ImageView go_back_img;
	public List<Map<String, Object>> mNavData;
	public Map<String, Object> mNavDataSelect;
	private LinearLayout blackboard_ll, class_record_ll, class_video_ll;
	private ImageView blackboard_img, class_record_img, class_video_img;
	private TextView blackboard_text, class_record_text, class_video_text;
	private LinearLayout prepare_all_ll,delete_time_ll;
	private ImageView time_search;
	private TextView select_time,currentSubject;
	private CommSearch commSearch;
	private ArrayList<SubjectObject>list=new ArrayList<SubjectObject>();

public static void startActivity(Context context){
	Intent intent=new Intent(context,ClassFileActivity.class);
	context.startActivity(intent);
}
	@Override
	protected void initView() {
		setContentView(R.layout.class_file_activity);
		go_back_img = (ImageView) findViewById(R.id.go_back_img);
		blackboard_img = (ImageView) findViewById(R.id.blackboard_img);
		class_record_img = (ImageView) findViewById(R.id.class_record_img);
		class_video_img = (ImageView) findViewById(R.id.class_video_img);

		blackboard_text = (TextView) findViewById(R.id.blackboard_text);
		class_record_text = (TextView) findViewById(R.id.class_record_text);
		class_video_text = (TextView) findViewById(R.id.class_video_text);
		select_time=(TextView) findViewById(R.id.select_time);
		currentSubject=(TextView) findViewById(R.id.classroom_chooseSubject);

		blackboard_ll = (LinearLayout) findViewById(R.id.blackboard_ll);
		class_record_ll = (LinearLayout) findViewById(R.id.class_record_ll);
		class_video_ll = (LinearLayout) findViewById(R.id.class_video_ll);
		prepare_all_ll = (LinearLayout) findViewById(R.id.prepare_all_ll);
		delete_time_ll=(LinearLayout) findViewById(R.id.delete_time_ll);
		time_search=(ImageView) findViewById(R.id.time_search);
		ClassOnFocusListener onFoucsClik = new ClassOnFocusListener();

		blackboard_ll.setOnFocusChangeListener(onFoucsClik);
		class_record_ll.setOnFocusChangeListener(onFoucsClik);
		class_video_ll.setOnFocusChangeListener(onFoucsClik);




	}

	@Override
	protected void initData() {
		super.initData();
		commSearch=new CommSearch(this);
		initListView();

	}

	@Override
	protected void initEvent() {
		super.initEvent();
		time_search.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				commSearch.showTimeWindow(delete_time_ll,select_time, CommonConfig.CLASSFILE_SEARCH);
			}
		});
		go_back_img.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				finish();
			}
		});
		prepare_all_ll.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				commSearch.showSubject(currentSubject, delete_time_ll, CommonConfig.CLASSFILE_SEARCH);
			}
		});
		delete_time_ll.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				delete_time_ll.setVisibility(View.GONE);
				commSearch.setStartTime(null,CommonConfig.CLASSFILE_SEARCH);
			}
		});
	}

	// 选项切换判断
	private class ClassOnFocusListener implements OnFocusChangeListener {
		@Override
		public void onFocusChange(View v, boolean hasFocus) {
			switch (v.getId()) {
			// 小黑板
			/*case R.id.blackboard_ll:
				if (hasFocus) {
					blackboard_img.setImageResource(R.mipmap.bord_1);
					blackboard_ll.setBackgroundColor(getResources().getColor(R.color.class_file_word));
					blackboard_text.setTextColor(0Xff7d7d7d);
					showFragment(mNavData.get(0));

				} else {
					blackboard_img.setImageResource(R.mipmap.board2);
					blackboard_ll.setBackgroundColor(getResources().getColor(R.color.class_file_leftbackground));
					blackboard_text.setTextColor(0Xffffffff);
				}
				break;*/

			// 课堂笔记
			case R.id.class_record_ll:
				if (hasFocus) {
					class_record_img.setImageResource(R.mipmap.choosepic);
					class_record_ll.setBackgroundColor(getResources().getColor(R.color.class_file_word));
					class_record_text.setTextColor(0Xff7d7d7d);
					showFragment(mNavData.get(0));
				} else {
					class_record_img.setImageResource(R.mipmap.classroom_file_pic);
					class_record_ll.setBackgroundColor(getResources().getColor(R.color.class_file_leftbackground));
					class_record_text.setTextColor(0Xffffffff);
				}
				break;

			}
		}
	}

	/**
	 * 显示内容
	 * 
	 * @param map
	 */
	public void showFragment(Map<String, Object> map) {
		mNavDataSelect = map;
		FragmentManager fragmentManager = getSupportFragmentManager();
		FragmentTransaction transaction = fragmentManager.beginTransaction();

		Fragment fragment = (Fragment) map.get("fragment");
		boolean isInit = Boolean.parseBoolean(map.get("isInit").toString());

		// 检查是否存在
		if (!isInit) {
			transaction.add(R.id.frament, fragment);
			map.put("isInit", true);
		}

		// 隐藏&显示控件
		for (Map<String, Object> item : mNavData) {
			Fragment itemFragment = (Fragment) item.get("fragment");
			if (itemFragment == null) {
				continue;
			}
			if (itemFragment.equals(fragment)) {
				transaction.show(itemFragment);
			} else {
				transaction.hide(itemFragment);
			}
		}
		transaction.commit();
	}

	private void initListView() {
		mNavData = new ArrayList<Map<String, Object>>();
		Map<String, Object> map = new HashMap<String, Object>();

		/*map = new HashMap<String, Object>();
		map.put("fragment", new ClassBoardRecordFragment());
		map.put("isInit", true);
		mNavData.add(map);*/

		map = new HashMap<String, Object>();
		map.put("fragment", new ClassNoticeRecordFragment());
		//map.put("fragment", new ClassRecordFragment());
		map.put("isInit", true);
		mNavData.add(map);

		this.initFragment();
	}


	private void initFragment() {
		FragmentManager fragmentManager = getSupportFragmentManager();
		FragmentTransaction transaction = fragmentManager.beginTransaction();

		for (Map<String, Object> item : mNavData) {
			Fragment itemFragment = (Fragment) item.get("fragment");
			boolean isInit = Boolean.parseBoolean(item.get("isInit").toString());
			if (isInit && itemFragment != null) {
				transaction.add(R.id.frament, itemFragment);
				transaction.hide(itemFragment);
			}
		}

		transaction.commit();
	}
	
}
